package cn.tedu.home1ess.admin.traffic.service.impl;

import cn.tedu.home1ess.admin.traffic.dao.repository.ITrafficRepository;
import cn.tedu.home1ess.admin.traffic.pojo.po.MapTrafficPO;
import cn.tedu.home1ess.admin.traffic.pojo.po.RecentTrafficPO;
import cn.tedu.home1ess.admin.traffic.pojo.po.TOPTrafficPO;
import cn.tedu.home1ess.admin.traffic.pojo.po.TrafficPO;
import cn.tedu.home1ess.admin.traffic.pojo.vo.MapTrafficVO;
import cn.tedu.home1ess.admin.traffic.pojo.vo.RecentTrafficVO;
import cn.tedu.home1ess.admin.traffic.pojo.vo.TopTrafficVO;
import cn.tedu.home1ess.admin.traffic.service.ITrafficService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class TrafficServiceImpl implements ITrafficService {
    @Autowired
    private ITrafficRepository trafficRepositoryImpl;

    @Override
    public List<MapTrafficVO> getMapTraffic() {
        List<MapTrafficVO> mapTrafficVOS = new ArrayList<>();
        List<MapTrafficPO> list = trafficRepositoryImpl.getMapTraffic();
        for (MapTrafficPO mapTrafficPO : list) {
            MapTrafficVO mapTrafficVO = new MapTrafficVO();
            BeanUtils.copyProperties(mapTrafficPO, mapTrafficVO);
            mapTrafficVOS.add(mapTrafficVO);
        }
        return mapTrafficVOS;
    }

    @Override
    public List<TopTrafficVO> getTopTraffic() {
        List<TopTrafficVO> topTrafficVOS = new ArrayList<>();
        List<TOPTrafficPO> list = trafficRepositoryImpl.getTopTraffic();
        for (TOPTrafficPO topTrafficPO : list) {
            TopTrafficVO topTrafficVO = new TopTrafficVO();
            BeanUtils.copyProperties(topTrafficPO, topTrafficVO);
            topTrafficVOS.add(topTrafficVO);
        }
        return topTrafficVOS;
    }

    @Override
    public List<RecentTrafficVO> getRecentTraffic() {
        List<RecentTrafficVO> recentTrafficVOS = new ArrayList<>();
        List<RecentTrafficPO> list = trafficRepositoryImpl.getRecentTraffic();
        for (RecentTrafficPO recentTrafficPO : list) {
            RecentTrafficVO recentTrafficVO = new RecentTrafficVO();
            BeanUtils.copyProperties(recentTrafficPO, recentTrafficVO);
            recentTrafficVOS.add(recentTrafficVO);
        }
        return recentTrafficVOS;
    }

    @Override
    public void userCount() {
        Date date = new Date();
        TrafficPO realTraffic = trafficRepositoryImpl.selectRealTime();
        Date realTime = realTraffic.getDate();
        if (realTime == date) {
            trafficRepositoryImpl.addUserCount(realTime);
        } else {
            TrafficPO trafficPO = new TrafficPO();
            trafficPO.setDate(date);
            trafficRepositoryImpl.createCount(trafficPO);
        }
    }

    @Override
    public void trafficCount() {
        Date date = new Date();
        TrafficPO realTraffic = trafficRepositoryImpl.selectRealTime();
        Date realTime = realTraffic.getDate();
        if (realTime == date) {
            trafficRepositoryImpl.addTrafficCount(realTime);
        } else {
            TrafficPO trafficPO = new TrafficPO();
            trafficPO.setDate(date);
            trafficRepositoryImpl.createCount(trafficPO);
        }
    }
}
